// pages/my/my.js
const app = getApp();
import {
  UserModels
} from "../../utils/models/user/User-models";
var userModel = new UserModels;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarHeight: app.globalData.navbarHeight,
    height: '',
    titleName: '个人中心',
    navBgColor: "yellow",
    isLeft: false,
    userinfo:{},
    orderCount:{},
    type: 1,
    is_copartner:0,
  },

  // 前往 “开通会员”
  goJoin: function() {
    wx.navigateTo({
      url: '../join_membership/join_membership',
    })
  },

  // 复制ID
  copyBtn: function (e) {
    var data = e.currentTarget.dataset.id;
    if (data) {
      wx.setClipboardData({
        //准备复制的数据
        data: data,
        success: function (res) {
          console.log(res)
        }
      });
    }
  },
  getPhoneNumber:function(e){
    var param = {
      'encryptedData': e.detail.encryptedData,
      'iv': e.detail.iv,
    };
    userModel.updateMobile(param,function(res){
      var userinfo = wx.getStorageSync('userinfo');
      userinfo.mobile = res.data.mobile;
      wx.setStorageSync('userinfo', userinfo)
    })
  },
  bindGetUserInfo:function(e){
    let userinfo = e.detail.userInfo;
    let user = this.data.userinfo;
    let that = this;
    userModel.editInfo(userinfo,function(res){
      user.headimgurl = res.data.headimgurl;
      user.nickname = res.data.nickname;
      console.log(user)
      that.setData({
        userinfo:user
      })
    })
  },
  getUserinfo:function(){
    var that = this;
    userModel.getUserinfo(function(res){
      var userinfo = res.data.user;
      userinfo.teamCount = res.data.teamCount;
      that.setData({
        'userinfo': userinfo,
        'orderCount':res.data.orderCount,
      })
    })
  },

  // 跳转分享赚
  goShare: function() {
    wx.switchTab({
      url: '../share/share',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserinfo();
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // var userinfo = wx.getStorageSync('userinfo');
    // app.login(userinfo);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function (res) {
    var loginInfo = wx.getStorageSync('userinfo');
    var invite_id = 0;
    if (loginInfo && loginInfo.user_id > 0) {
      invite_id = loginInfo.user_id;
    }
   
    //判断是否通过点击按钮分享
    if (res.from == 'button') {
      return {
        path: '/pages/share/share?invite_id=' + invite_id
      }
    }
  }
})